/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.edu.amu.wmi.kino.visualjavafx.visualjavafxproject.mimeFileType;

import java.io.BufferedReader;
import pl.edu.amu.wmi.kino.visualjavafx.model.api.ModelChangeEvent;
import pl.edu.amu.wmi.kino.visualjavafx.model.api.ModelChangeListener;
import pl.edu.amu.wmi.kino.visualjavafx.model.api.VisualJavaFxModelProvider;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Iterator;
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataObjectExistsException;
import org.openide.loaders.MultiDataObject;
import org.openide.loaders.MultiFileLoader;
import org.openide.nodes.Node;
import org.openide.util.Lookup;
import org.openide.util.lookup.AbstractLookup;
import org.openide.util.lookup.InstanceContent;
import org.openide.util.lookup.ProxyLookup;
import pl.edu.amu.wmi.kino.visualjavafx.javafxcodeparsers.api.JavaFxCodeParser;
import pl.edu.amu.wmi.kino.visualjavafx.model.objects.Application;

public class VisualJavaFxFileDataObject extends MultiDataObject {

    final InstanceContent ic = new InstanceContent();
    private Application model = null;
    private Lookup lookup = new ProxyLookup( new Lookup[] {getCookieSet().getLookup() , new AbstractLookup(ic)} );
    private VisualJavaFxModelProvider provider = new ModelProvider();

    public VisualJavaFxFileDataObject(FileObject pf, MultiFileLoader loader) throws DataObjectExistsException, IOException {
        super(pf, loader);
        InputStreamReader isr = new InputStreamReader(pf.getInputStream());
        Reader r = new BufferedReader(isr);
        StringBuffer buf = new StringBuffer();
            int ch;
            while ((ch = r.read()) > -1) {
                buf.append((char)ch);
            }
            r.close();
        String s = buf.toString();
        model = JavaFxCodeParser.parseCode(s);
        ic.add(VisualJavaFxFileEditorSupport.create(this));
        ic.add(this);
        ic.add(provider);
    }

    @Override
    protected Node createNodeDelegate() {
        return new VisualJavaFxFileDataNode(this, getLookup());
    }

    @Override
    public Lookup getLookup() {
        return lookup;
    }

    @Override
    public Node.Cookie getCookie(Class type) {
        Object o = lookup.lookup(type);
        return o instanceof Node.Cookie ? (Node.Cookie)o : null;
    }
    
    private class ModelProvider implements VisualJavaFxModelProvider{

        private ArrayList<ModelChangeListener> listeners = new ArrayList<ModelChangeListener>();

        @Override
        public Application getModel() {
            return model;
        }

        @Override
        public void addModelChangeListener(ModelChangeListener listener) {
            if (!listeners.contains(listener)){
                listeners.add(listener);

            }
        }

        @Override
        public void notifyModelChange(ModelChangeEvent evt) {
            Object[] who = evt.getWho();

            for (int i = 0; i < who.length; i++) {
                if (who[i] == this) return;
            }

            for (Iterator<ModelChangeListener> it = listeners.iterator(); it.hasNext();) {
                ModelChangeListener modelChangeListener = it.next();
                modelChangeListener.notifyModelChange(ModelChangeEvent.passEvent(evt, this));
            }

        }

        @Override
        public void removeModelChangeListener(ModelChangeListener listener) {
            listeners.remove(listener);
        }
    }


}